﻿using System;
using System.Linq;

namespace MinesweeperUtility
{
    public class MainMenu : IDraw
    {
        //STRUCT : PLAYER


        private string empty10Spaces = new string(' ', 10);
        private string empty20Spaces = new string(' ', 20);
        private string empty40Spaces = new string(' ', 40);

        private Level difficulty;
        private string playerName;

        public Level Difficulty { get { return this.difficulty; } }
        public string PlayerName { get { return this.playerName; } }

        private void ClearRow(int row)
        {
            Console.SetCursorPosition(21, row);
            Console.Write(empty20Spaces + "║" + empty40Spaces);
            Console.SetCursorPosition(21, row);
        }

        public void Draw(int startX, int startY)
        {
            Console.Write("╔");
            Console.Write(new string('═', 57));
            Console.WriteLine("╗");

            Console.WriteLine("║" + empty10Spaces + " Enter difficulty settings :" + new string(' ', 19) + "║");
            Console.WriteLine("║" + empty10Spaces + " 0 or Beginner" + new string(' ', 33) + "║");
            Console.WriteLine("║" + empty10Spaces + " 1 or Intermediate" + new string(' ', 29) + "║");
            Console.WriteLine("║" + empty10Spaces + " 2 or Expert" + new string(' ', 35) + "║");

            Console.WriteLine("║" + empty10Spaces + " Enter your name:" + new string(' ', 30) + "║");
            Console.WriteLine("║" + empty10Spaces + " 1 word, up to 12 symbols." + new string(' ', 21) + "║");
            Console.WriteLine("║" + empty10Spaces + " If your name is too long, use a nickname" + new string(' ', 6) + "║");

            Console.Write("╚");
            Console.Write(new string('═', 57));
            Console.WriteLine("╝");

            Console.Write(empty20Spaces + "╔");
            Console.Write(new string('═', 20));
            Console.WriteLine("╗");

            Console.WriteLine(empty20Spaces + "║" + empty20Spaces + "║");
            Console.WriteLine(empty20Spaces + "║" + "Difficulty:" + new string(' ', 9) + "║");
            Console.WriteLine(empty20Spaces + "║" + empty20Spaces + "║");
            Console.WriteLine(empty20Spaces + "║" + "Name:" + new string(' ', 15) + "║");
            Console.WriteLine(empty20Spaces + "║" + empty20Spaces + "║");

            Console.Write(empty20Spaces + "╚");
            Console.Write(new string('═', 20));
            Console.Write("╝");
        }

        public void GameInfo()
        {
            this.Draw(0, 0);
            bool exceptionCheck = true;
            while (exceptionCheck)
            {
                try
                {
                    ClearRow(12);
                    string level = Console.ReadLine();

                    while (exceptionCheck)
                    {
                        while (level == "")
                        {
                            ClearRow(12);
                            level = Console.ReadLine();
                        }
                        if (level != "Beginner" && level != "Intermediate" && level != "Expert")
                        {
                            try
                            {
                                int.Parse(level);
                                exceptionCheck = false;
                                if (int.Parse(level) > 2 || int.Parse(level) < 0)
                                {
                                    exceptionCheck = true;
                                    throw new Exception();
                                }
                            }
                            catch (Exception)
                            {
                                ClearRow(12);
                                level = Console.ReadLine();
                            }
                        }
                        else
                        {
                            exceptionCheck = false;
                        }
                    }
                    exceptionCheck = true;

                    this.difficulty = (Level)Enum.Parse(typeof(Level), level);
                    exceptionCheck = false;
                }
                catch (ArgumentException)
                {
                }
            }

            Console.SetCursorPosition(21, 14);
            this.playerName = Console.ReadLine();
            while (playerName.Length > 12 || playerName == "")
            {
                ClearRow(14);
                this.playerName = Console.ReadLine();
            }

            this.playerName = this.playerName.ToLower();
            this.playerName = this.playerName[0].ToString().ToUpper() + this.playerName.Substring(1);
            Console.Clear();
        }

    }
}